//用户登录检测
var token = null;
var userinfo = null;
var headpic = null;
var level_id = 0;
var username = 'gh_96c7706f8ae6';
var from = 'XIAOFANG';
var major_type = 'LEVEL_1';
var text = '消防工程师';
var host = "https://api.laoyaojing.net";

if (!(typeof($api.getStorage('text')) == "undefined") || !($api.getStorage('text') == null)) {
  text = $api.getStorage('text');
  console.log(text);
}

if (!(typeof($api.getStorage('username')) == "undefined") || !($api.getStorage('username') == null)) {
  username = $api.getStorage('username');
  console.log(text);
}

console.log($api.getStorage('level_id'));

if (!(typeof($api.getStorage('level_id')) == "undefined") || !($api.getStorage('level_id') == null) ) {
  level_id = $api.getStorage('level_id');
  console.log(level_id);
}

if (!(typeof($api.getStorage('from')) == "undefined") || !($api.getStorage('from') == null) ) {
  from = $api.getStorage('from');
}

  console.log(from);

if (!(typeof($api.getStorage('major_type')) == "undefined") ||!($api.getStorage('major_type') == null) ) {
  major_type = $api.getStorage('major_type');
}
console.log(major_type);

if (!(typeof($api.getStorage('host')) == "undefined") || !($api.getStorage('host') == null) ) {
  host = $api.getStorage('host');
}
console.log(host);


token = $api.getStorage('token');
if (typeof(token) == "undefined" || token == null) {
  token = null;
  userinfo = null;
  headpic = null;
}
else{
    userinfo = $api.getStorage('userinfo');
    headpic = $api.getStorage('headpic');
}
console.log(headpic);
console.log(JSON.stringify(userinfo));
// userinfo = 123;
function closeWin()
{
  api.closeWin();
}

//微信登陆
function login(type) {
  wx.isInstalled(function(ret, err) {
      console.log(JSON.stringify(ret));
      if (ret.installed) {
          wx.auth({apiKey:'wx86c4a72d01af74a0'}, function(ret, err) {
              if (ret.status) {
                  console.log(JSON.stringify(ret));
                  var code = ret.code;
                  wx.getToken({
                      apiKey: '',
                      apiSecret: '',
                      code: code
                  }, function(ret, err) {
                    console.log(JSON.stringify(ret));
                      if (ret.status) {

                          var accessToken = ret.accessToken;
                          var openId = ret.openId;
                          wx.getUserInfo({
                              accessToken: accessToken,
                              openId: openId
                          }, function(ret, err) {
                              if (ret.status) {

                                  $api.setStorage('userinfo', ret);

                                  var nickname = ret.nickname;
                                  var avatar = ret.headimgurl;

                                  api.imageCache({
                                      url: ret.headimgurl
                                  }, function(ret, err) {
                                      var url = ret.url;
                                      console.log(url);
                                      $api.setStorage('headpic', url);
                                  });
                                  console.log(ret);
                                  api.ajax({
                                      url: 'https://api.laoyaojing.net/Public/doAppLogin',
                                      method: 'post',
                                      data: {
                                          values: {
                                              openid: ret.openid,
                                              unionid:ret.unionid,
                                              nickname:nickname,
                                              avatar:avatar,
                                              app:from,
                                              major_type:major_type
                                          }
                                      }
                                  },function(ret, err){
                                      console.log(JSON.stringify(ret));
                                      if (ret) {

                                          console.log(ret);
                                          if (ret.code == 200) {
                                            $api.setStorage('token', ret.result.token);
                                            console.log(ret.result.token);
                                            console.log(type);
                                            if (type == 1) {
                                                api.execScript({
                                                    name: 'root',
                                                    frameName: 'mine',
                                                    script: 'window.location.reload();'
                                                });

                                            }
                                            else{
                                              api.execScript({
                                                  name: 'root',
                                                  frameName: 'mine',
                                                  script: 'window.location.reload();'
                                              });

                                              api.openWin({
                                                  name: 'choose-level',
                                                  reload: true,
                                                  url: 'choose-level.html',
                                                  slidBackEnabled:false
                                              });
                                            }
                                          }
                                          // alert( JSON.stringify( ret ) );
                                      } else {
                                          alert( JSON.stringify( err ) );
                                      }
                                  });



                              } else {
                                  alert(err.code);
                              }
                          });
                      } else {
                          alert(err.code);
                      }
                  });
              } else {
                  alert(err.code);
              }
          });
      } else {
          alert('当前设备未安装微信客户端');
      }
  });
}

function loginOut()
{
  $api.rmStorage('token');
  $api.rmStorage('userinfo');
  $api.rmStorage('headpic');

  api.execScript({
      name: 'root',
      script: 'setFrameGroup(0)'
  });

  api.execScript({
      name: 'root',
      frameName: 'mine',
      script: 'window.location.reload();'
  });

  api.openWin({
      name: 'login',
      reload: true,
      url: './login.html',
  });
}

function getMiniId(){
  if(from == 'XIAOFANG' ){
        $api.setStorage('username', 'gh_96c7706f8ae6');
  }else if(from == 'REGISTERED_CONSTRUCTOR'){
        $api.setStorage('username', 'gh_a61b7c1ec70e');
  }else if(from == 'GPS'){
        $api.setStorage('username', 'gh_de01761f55dc');
  }else{

  }
}
